"""
@FileName：pc.py\n
@Description：\n
@Author：陈雨杰、封玲菲\n
@Time：2024/4/8 11：51\n
@Copyright：©2019-2024 研发中心
"""
import scrapy
from lxml import etree

from ..items import global_stock_indices

class PcSpider(scrapy.Spider):
    name = "global_stock_indices"
    allowed_domains = ["data.eastmoney.com"]
    start_urls = ["https://quote.eastmoney.com/center/gridlist.html#global_australia"]
    custom_settings = {
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.ConsolePipeline': 300},
        'ITEM_PIPELINES': {'爬虫.eastmoney.eastmoney.pipelines.MysqlPipeline': 200},
    }

    def parse(self, response):
        html = etree.HTML(response.text)
        a_list = html.xpath('//*[@id="tab"]/ul/li/a/@href')
        for a in a_list:
            detail_url = response.urljoin(a)
            yield scrapy.Request(
                url=detail_url,
                callback=self.parse_detail,
                dont_filter=True
            )
    def parse_detail(self, response):

        item = global_stock_indices()
        html = etree.HTML(response.text)

        table = html.xpath("//table[@id='table_wrapper-table']/tbody/tr")
        def value_empty(value_):
            if (value_):
                return value_[0].replace("%","")
            else:
                return 0

        for tr in table:
            # 提取文本

               arr = tr.xpath('.//td')
               item['country_name'] = arr[1].xpath('.//a//text()')
               item['latest_price'] = arr[2].xpath('.//span/text()')
               item['rising_falling_amounts'] = arr[3].xpath('.//span/text()')

               item['percentage_change'] = value_empty(arr[4].xpath('.//span/text()'))

               item['opening_price'] = arr[5].xpath('.//span/text()')
               item['maximum_price'] = arr[6]. xpath('.//span/text()')
               item['bottom_price'] = arr[7].xpath('.//span/text()')
               item['closing_price'] = arr[8].xpath('./text()')

               item['amplitude'] =value_empty( arr[9].xpath('./text()'))

               yield item

